<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt" style="margin-bottom: 10px;display: none;">
			<uni-segmented-control :current="current" :values="items" :style-type="styleType" :active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view class="uni-common-mt" v-if="current === 0">
			<form>
				<view class="uni-form-item uni-column">
					<uni-section title="基本信息" type="line"></uni-section>
					<view class="example-body" style="">
					
					</view>
				</view>
				<view class="uni-list list-pd">
						<view class="uni-list-cell cell-pd">
							<view class="uni-uploader">
								<view class="uni-uploader-head">
									<view class="uni-uploader-title"><text class="t-fill">*</text>商品图片</view>
									<view class="uni-uploader-info">{{imageList.length}}/9</view>
								</view>
								<view class="uni-uploader-body">
									<view class="uni-uploader__files">
										<block v-for="(image,index) in imageList" :key="index">
											<view class="uni-uploader__file">
												<image class="uni-uploader__img" :src="image" :data-src="image" @tap="previewImage"></image>
												<view class="v-img-bak"></view>
												<image class="image-left" :src="imageLeft" :data-src="index" @tap="toLeftImage"></image>
												<image class="image-right" :src="imageRight" :data-src="index" @tap="toRightImage"></image>
												<image class="image-del" :src="imageDel" :data-src="index" @tap="delImage"></image>												
												
											</view>
										</block>
										<view class="uni-uploader__input-box">
											<view class="uni-uploader__input" @tap="chooseImage"></view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				
				<view class="uni-common-mt">
					<view class="uni-form-item uni-column">
					    <view class="title"><text class="t-fill">*</text><text class="uni-form-item__title">商品名称</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" maxlength="50" :value="productName" placeholder="例如: 鲜榨果汁500ml" @blur="changeProductName"  />
					    </view>
					</view>
					<view class="uni-form-item uni-column">
					    <view class="title"><text class="uni-form-item__title">商品卖点</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" maxlength="100" :value="productSellPoint" placeholder="例如: 美国进口果源" @blur="changeProductSellPoint"  />
					    </view>
					</view>
					<view class="uni-form-item uni-column">
						<view class="title"><text class="uni-form-item__title">商品分组</text>
							<view style="margin-left: 20px;display: inline;"><text style="color: #007AFF;"  @click="toSelectProductGroup">选择</text><text style="color: #007AFF;margin-left: 15px;"  @click="toAddProductGroup">新建</text></view>
						</view>
						<view class="uni-list">
						<view class="uni-list-cell" style="min-height: 38px;">
							<view class="uni-list-cell-left">
								当前选择
							</view>
							<view class="uni-list-cell-db">
								<view class="v-select-mul-item">
									<view class="v-select-mul-con" v-for="(item,index) in groupList" :key="index" >
										<view class="t-select-mul-value">{{item.name}}<text @click="delSelectProGroupValue(item.name)" style="font-size: 18px;color: #ff4444;margin-left: 6px;">x</text></view>
									</view>
								</view>
							</view>
						</view>
						</view>
					</view>
					<view class="uni-form-item uni-column" style="display: none;">
						<view class="title"><text class="uni-form-item__title">库存单位</text></view>
						<view class="uni-list">
						<view class="uni-list-cell">
							<view class="uni-list-cell-left">
								当前选择
							</view>
							<view class="uni-list-cell-db">
								<picker @change="bindUnitPickerChange" :value="unitIndex" :range="unitArray" range-key="name">
									<view class="uni-input">{{unitArray[unitIndex].name}}</view>
								</picker>
							</view>
						</view>
						</view>
					</view>
					
					<view class="uni-form-item uni-column">
						<uni-section title="规格信息(SKU)" type="line"></uni-section>
						<view class="example-body" style="">
							<view class="uni-form-item uni-column">
								<view v-for="(tmp,index) in skuArray" :key="index">
								<view class="uni-flex uni-row" style="margin-top: 10rpx;">
									<view class="title" style="width: 168rpx;"><text class="t-fill">*</text>规格名称</view>
									<view class="text" style="-webkit-flex: 1;flex: 1;"><input class="uni-input" name="nickname" placeholder="请输入规格名称" :data-src="index" @input="changeSkuName" :value="tmp.name" /></view>
									<view class="text" style="width: 100rpx;"><image class="" style="position: relative;width: 60rpx;height: 60rpx;top:10% ;left:20%" :src="skuDel" :data-src="index" @tap="delSku"></image>	</view>
								</view>
								<view class="uni-flex uni-row" style="margin-top: 10rpx;">
									<view class="title" style="width: 168rpx;"><text class="t-fill">*</text>产品属性</view>
									<view class="text" style="-webkit-flex: 1;flex: 1;"><input class="uni-input" name="nickname" placeholder="多个产品属性以#隔开" :data-src="index" @input="changeSkuPro" :value="tmp.pro" /></view>
									<view class="text" style="width: 100rpx;"></view>
								</view>	
								</view>
								<view class="uni-flex uni-row" style="margin-top: 20rpx;">
									<view class="title" style="width: 168rpx;">规格设置</view>
									<view class="text" style="-webkit-flex: 1;flex: 1;"><button class="mini-btn" type="default" size="mini" style="height: 60rpx;margin-top: 5rpx;" @click="addSku">添加规格</button></view>
									<view class="text" style="width: 100rpx;"></view>
								</view>	
							</view>
							<view v-if="skuArray.length>0" v-for="(tmp,index) in skuDetailArray" :key="index">
								<view class="uni-form-item uni-column">
									<view class="title"><text class="uni-form-item__title">{{ tmp.name }}</text></view>
									<view class="uni-list">
									<view class="uni-list-cell">
										<view class="uni-list-cell-left">
											<text class="t-fill">*</text>零售价格
										</view>
										<view class="uni-list-cell-db">
												<view class="uni-input"><input class="uni-input2" :value="tmp.retailPrice" name="nickname"  type="digit"  placeholder="请输入零售价格" :data-src="index" @input="changeSkuDetailRetail" /></view>
										
										</view>
									</view>
									<view class="uni-list-cell">
										<view class="uni-list-cell-left">
											<text class="t-fill">*</text>库存
										</view>
										<view class="uni-list-cell-db">
												<view class="uni-input"><input class="uni-input2" :value="tmp.stock" name="nickname"  type="digit"  placeholder="请输入商品库存" :data-src="index" @input="changeSkuDetailStock" /></view>
										
										</view>
									</view>
									<view class="uni-list-cell">
										<view class="uni-list-cell-left">
											重量
										</view>
										<view class="uni-list-cell-db">
												<view class="uni-input"><input class="uni-input2" :value="tmp.weight" name="nickname" type="digit"  placeholder="请输入重量(Kg)" :data-src="index" @input="changeSkuDetailWeight" /></view>
										
										</view>
									</view>
									<view class="uni-list-cell">
										<view class="uni-list-cell-left">
											成本价格
										</view>
										<view class="uni-list-cell-db">
												<view class="uni-input"><input class="uni-input2" :value="tmp.costPrice" name="nickname" type="digit"  placeholder="请输入成本价格" :data-src="index" @input="changeSkuDetailCost" /></view>
										
										</view>
									</view>
									
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="uni-form-item uni-column">
					    <view class="title"><text class="t-fill">*</text><text class="uni-form-item__title">建议零售价</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" :value="skuRetailPrice" maxlength="10" type="digit" :disabled="skuArray.length>0?true:false" placeholder="人民币" @input="changeSkuRetail" />
					    </view>
					</view>

					<view class="uni-form-item uni-column">
					    <view class="title"><text class="t-fill">*</text><text class="uni-form-item__title">库存</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" :value="skuStock" maxlength="10" type="digit"  :disabled="skuArray.length>0?true:false"  placeholder="请输入商品库存" @input="changeSkuStock" />
					    </view>
					</view>
					
					<view v-if="skuArray.length==0">
					
					<view class="uni-form-item uni-column">
					    <view class="title"><text class="uni-form-item__title">重量</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" :value="skuWeight" maxlength="10" type="digit"  placeholder="Kg" @input="changeSkuWeight" />
					    </view>
					</view>
					
					<view class="uni-form-item uni-column">
					    <view class="title"><text class="uni-form-item__title">成本价</text></view>
					    <view class="uni-input-wrapper">
					        <input class="uni-input" :value="skuCostPrice" maxlength="10" type="digit"  placeholder="人民币" @input="changeSkuCost" />
					    </view>
					</view>
					
					</view>
					
					<view class="uni-form-item uni-column">
						<uni-section title="其他信息" type="line"></uni-section>
						<view class="example-body" style="">
							<view class="uni-flex uni-row" style="margin-top: 10rpx;">
								<view class="title" style="width: 168rpx;"><text class="t-fill">*</text>配送方式</view>
								<view class="text" style="-webkit-flex: 1;flex: 1;margin-top: 5px;">
									<checkbox-group @change="changeDeliveryValue">
										<label>
											<checkbox value="快递发货" :checked="delivery.indexOf('快递发货')==-1?false:true" />快递发货
										</label>
										<label>
											<checkbox value="到店自提" :checked="delivery.indexOf('到店自提')==-1?false:true" />到店自提
										</label>
									</checkbox-group>
								</view>
							</view>
							
							<view class="uni-flex uni-row" style="margin-top: 10rpx;" v-if="delivery.indexOf('快递发货')!=-1">
								<view class="title" style="width: 168rpx;"><text class="t-fill">*</text>快递运费</view>
								<view class="text" style="-webkit-flex: 1;flex: 1;margin-top: 5px;">
									<radio-group name="gender" @change="changeExpressValue">
										<label>
											<radio value="统一邮费" :checked="express=='统一邮费'?true:false" /><text>统一邮费</text>
										</label>
										<label>
											<radio value="快递模板" :checked="express=='快递模板'?true:false" /><text>快递模板</text>
										</label>
									</radio-group>
								</view>
							</view>
							<view class="uni-input-wrapper" v-if="express=='统一邮费'&&delivery.indexOf('快递发货')!=-1">
							    <input class="uni-input" :value="expressFee" maxlength="10" placeholder="请输入统一邮费(人民币)" type="digit"  @input="changeExpressFee" />
							</view>
							<view class="uni-list" v-if="express=='快递模板'&&delivery.indexOf('快递发货')!=-1">
							<view class="uni-list-cell">
								<view class="uni-list-cell-left">
									选择模板
								</view>
								<view class="uni-list-cell-db" v-if="expressTemArray.length>0">
									<picker @change="changeExpressTemValue" :value="expressTemIndex" :range="expressTemArray" range-key="name">
										<view class="uni-input">{{expressTemArray[expressTemIndex].name}}</view>
									</picker>
								</view>
								<view class="uni-list-cell-db" style="padding-top: 8px;padding-bottom: 8px;padding-left: 18px;" v-else>
									空
								</view>
							</view>
							
							</view>
							<view class="uni-form-item uni-column">
							<view class="uni-flex uni-row" style="margin-top: 10rpx;">
								<view class="title" style="width: 168rpx;">显示状态</view>
								<view class="text" style="-webkit-flex: 1;flex: 1;margin-top: 5px;">
									<radio-group name="state" @change="changeShowStateValue">
										<label>
											<radio value="上架" :checked="showState=='上架'?true:false" /><text>上架</text>
										</label>
										<label>
											<radio value="下架" :checked="showState=='下架'?true:false" /><text>下架</text>
										</label>
									</radio-group>
								</view>
							</view>
							</view>
						</view>
					</view>
					<view class="uni-padding-wrap uni-common-mt">
					    <button type="default" @click="toSubmit">提交</button>
						<button type="primary" @click="toEditProDetail" style="margin-top: 10px;">下一步(商品详情)</button>
					</view>
				</view>

				</form>
		</view>
		<view v-if="current === 1">商品详情</view>
		<zx-goods-group  @send="setSelectProGroupValue" ref="zxGoodsGroup"></zx-goods-group>
	</view>
</template>
<script>
	import uniNumberBox from '@/components/uni-ui/uni-number-box/uni-number-box.vue'
	import uniSection from '@/components/uni-ui/uni-section/uni-section.vue'
	import zxGoodsGroup from '@/components/zx-diy/zx-goods-group/zx-goods-group.vue'
	import uniSegmentedControl from '@/components/uni-ui/uni-segmented-control/uni-segmented-control.vue'
	import permision from "@/common/js/permission.js"
	import myCloud from '@/common/js/cloud.js';
	import myConst from '@/common/js/const.js'
	import myUtil from '@/common/js/util.js';
	var sourceType = [
		['camera'],
		['album'],
		['camera', 'album']
	]
	var sizeType = [
		['compressed'],
		['original'],
		['compressed', 'original']
	]
	export default {
		components: {
			uniNumberBox,
			uniSection,
			zxGoodsGroup,
			uniSegmentedControl
		},
		data() {
			return {
				id:'',
				nav:'back',
				action:'',
				title: 'choose/previewImage',
				imageList: [],
				groupList: [],
				cloudPath: [],
				sourceTypeIndex: 2,
				sourceType: ['拍照', '相册', '拍照或相册'],
				sizeTypeIndex: 2,
				sizeType: ['压缩', '原图', '压缩或原图'],
				countIndex: 8,
				count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
				imageDel:'/static/img/del.png',
				imageRight:'/static/img/right.png',
				imageLeft:'/static/img/left.png',
				skuDel:'/static/img/del2.png',
				unit:{id:1,name:'件'},
				unitIndex: 0,
				unitArray: [{id:1,name:'件'},{id:2,name: '只'}, {ud:3,name:'瓶'}, {id:4,name:'袋'}],
				numberValue: 0,
				productName: '',
				productSellPoint: '',
				skuArray: [],
				skuDetailArray: [],
				skuRetailPrice:'',
				skuCostPrice:'',
				skuWeight:'',
				skuStock:'',
				express:'统一邮费',
				expressFee:'',
				expressTemIndex:0,
				expressTemArray:[],
				expressTemSelected:{id:'',name:'',billingMode:''},
				skuPro:'',
				delivery: ['快递发货'],
				items: ['编辑基本信息', '编辑商品详情'],
				activeColor: '#007aff',
				current: 0,
				styleType: 'button',
				showState:'上架',
				submitData:{},
				sales:0,
				proDetail:[]
			}
		},
		onLoad(option) {
			if(option.id!=undefined){
				this.id = option.id;
				this.action = myConst.ACTION.EDIT_BY_NAME;
				const data = {
					action: myConst.ACTION.QUERY,
					params:{
						type:3,id:this.id
					}
				}
				myCloud.callFunction(myConst.FUNC_NAME.B_GOODS,data,(res,e)=>{
					this.proDetail = res[0].proDetail;
					let proData = res[0].proData;
					this.id = res[0]._id;
					this.productName = proData.productName;  //商品名称
					this.productSellPoint = proData.productSellPoint;  //商品卖点
					this.unit = proData.unit;  //单位
					this.skuArray = proData.skuArray;  
					this.skuDetailArray = proData.skuDetailArray;
					this.skuRetailPrice = proData.skuRetailPrice;  //建议零售价格
					this.skuCostPrice = proData.skuCostPrice;   //成本价
					this.skuStock = proData.skuStock;
					this.expressFee = proData.expressFee;  //统一运费
					this.express = proData.express;   //快递运费
					this.imageList = proData.imageList;
					this.cloudPath = proData.imageList;
					this.delivery = proData.delivery;    //配送方式
					this.showState = proData.showState;  //显示状态
					this.groupList = proData.groupList;  //商品分组
					this.skuWeight = proData.skuWeight;  //商品重量
					this.sales = proData.sales;
					this.expressTemSelected = proData.expressTemSelected;  //运费模板
					for(let item in this.unitArray){
						if(this.unitArray[item].id==this.unit.id){
							this.unitIndex = item;
							break;
						}
					}
					
					
				},true)
			}else{
				this.action = myConst.ACTION.ADD;
			}
			this.getInitData();
		},
		onUnload() {
			this.imageList = [],
				this.sourceTypeIndex = 2,
				this.sourceType = ['拍照', '相册', '拍照或相册'],
				this.sizeTypeIndex = 2,
				this.sizeType = ['压缩', '原图', '压缩或原图'],
				this.countIndex = 8;
		},
		methods: {
			formSubmit: function(e) {
				this.submitData = {};
				let tempImgList = [];
				let filePath = this.imageList;
				if (this.productName.length < 1) {
					uni.showToast({
						title: '请填写商品名称',
						icon: 'none'
					});
					return false;
				}
				if (this.imageList.length < 1) {
					uni.showToast({
						title: '请添加商品图片',
						icon: 'none'
					});
					return false;
				}
				for(let i=0;i<this.skuArray.length;i++){
					if(this.skuArray[i].name==""||this.skuArray[i].name==null){
						uni.showToast({
							title: '请填写规格名称',
							icon: 'none'
						});
						return false;
					}
					if(this.skuArray[i].pro==""||this.skuArray[i].pro==null){
						uni.showToast({
							title: '请填写产品属性',
							icon: 'none'
						});
						return false;
					}
				}
				for(let i=0;i<this.skuDetailArray.length;i++){
					if(this.skuDetailArray[i].retailPrice==""||this.skuDetailArray[i].retailPrice==null){
						uni.showToast({
							title: '请填写零售价格',
							icon: 'none'
						});
						return false;
					}
				}
				if(this.skuRetailPrice==""||this.skuRetailPrice==null){
					uni.showToast({
							title: '请填写建议零售价格',
							icon: 'none'
						});
						return false;
				}
				for(let i=0;i<this.skuDetailArray.length;i++){
					if(this.skuDetailArray[i].stock==""||this.skuDetailArray[i].stock==null){
						uni.showToast({
							title: '请填写库存',
							icon: 'none'
						});
						return false;
					}
				}
				if(this.skuStock==""||this.skuStock==null){
					uni.showToast({
							title: '请填写库存',
							icon: 'none'
						});
						return false;
				}
				if(this.delivery.length==0){
					uni.showToast({
							title: '请选择配送方式',
							icon: 'none'
						});
						return false;
				}
				if(this.delivery.indexOf('快递发货')!=-1){
					if(this.express=='统一邮费'){
						if(this.expressFee==''||this.expressFee==null){
							uni.showToast({
									title: '请输入统一邮费',
									icon: 'none'
								});
								return false;
						}
					}
					if(this.express=='快递模板'){
						if(this.expressTemSelected.billingMode=='按重量'){
							for(let i=0;i<this.skuDetailArray.length;i++){
								if(this.skuDetailArray[i].weight==""||this.skuDetailArray[i].weight==null){
									uni.showToast({
										title: '请填写重量',
										icon: 'none'
									});
									return false;
								}
							}
							if(this.skuDetailArray.length==0)
								if(this.skuWeight==''||this.skuWeight==null){
									uni.showToast({
										title: '请填写重量',
										icon: 'none'
									});
									return false;
								}
						}
					}
				}
				
				
				uni.showLoading({
					title: '上传图片',
					mask: false
				});
				let mCount = 0;
				for (let i = 0; i < filePath.length; i++) {
					if(filePath[i].indexOf("http://localhost")!=-1){
						mCount++;
					}
				}
				let mCount2 = 0;
				for (let i = 0; i < filePath.length; i++) {
					if(filePath[i].indexOf("http://localhost")==-1){
						mCount2++;
						if(mCount2==filePath.length){
							this.toAddProduct(filePath);
						}	
					}else{
						myCloud.uploadFile(filePath[i], this.cloudPath[i].name,(res)=>{
							filePath[i] = res.fileID;
							tempImgList.push(res.fileID);
							if (tempImgList.length == mCount) {
								// uni.hideLoading();
								this.toAddProduct(filePath);
							}
						})
					}
					
				}
			},
			toAddProduct(filePath){
				let tempData = {
					productName: this.productName,
					productSellPoint: this.productSellPoint,
					unit: this.unit,
					skuArray: this.skuArray,
					skuDetailArray: this.skuDetailArray,
					skuRetailPrice: this.skuRetailPrice,
					skuCostPrice:this.skuCostPrice,
					skuStock:this.skuStock,
					expressFee:this.expressFee,
					delivery:this.delivery,
					showState:this.showState,
					express:this.express,
					expressTemSelected:this.expressTemSelected,
					skuWeight:this.skuWeight,
					groupList:this.groupList,
					sales:this.sales,
					imageList: filePath
				};
				this.submitData = tempData;
				this.add_product();
			},
			add_product() {
				var that = this;
				const data = {
					action: that.action,
					params:{
						id:that.id,
						proData:that.submitData,
						proDetail:that.proDetail,
						updatetime:new Date().getTime()
					}
				}
				myCloud.callFunction(myConst.FUNC_NAME.B_GOODS,data,(res,e)=>{
					let tmpId = that.id==''?res.id:that.id
					uni.showToast({
						title: '提交成功',
						success:()=>{
							    if(this.nav=='next'){
									let tmp = 'proDetailDiy'
									uni.redirectTo({
										url: '/pages/shop/page-diy?pageName='+tmp+'&id='+tmpId
									})
								}else{
									uni.navigateBack()
								}
							}
						});
				},true)
			},
			chooseImage: async function() {
				// #ifdef APP-PLUS
				// TODO 选择相机或相册时 需要弹出actionsheet，目前无法获得是相机还是相册，在失败回调中处理
				if (this.sourceTypeIndex !== 2) {
					let status = await this.checkPermission();
					if (status !== 1) {
						return;
					}
				}
				// #endif

				if (this.imageList.length === 9) {
					let isContinue = await this.isFullImg();
					if (!isContinue) {
						return;
					}
				}
				uni.chooseImage({
					sourceType: sourceType[this.sourceTypeIndex],
					sizeType: sizeType[this.sizeTypeIndex],
					count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
					success: (res) => {
						this.imageList = this.imageList.concat(res.tempFilePaths);
						this.cloudPath = this.cloudPath.concat(res.tempFiles);
					},
					fail: (err) => {
						// #ifdef APP-PLUS
						if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
							this.checkPermission(err.code);
						}
						// #endif
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = false;
								switch (this.sourceTypeIndex) {
									case 0:
										authStatus = res.authSetting['scope.camera'];
										break;
									case 1:
										authStatus = res.authSetting['scope.album'];
										break;
									case 2:
										authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
										break;
									default:
										break;
								}
								if (!authStatus) {
									uni.showModal({
										title: '授权失败',
										content: 'Hello uni-app需要从您的相机或相册获取图片，请在设置界面打开相关权限',
										success: (res) => {
											if (res.confirm) {
												uni.openSetting()
											}
										}
									})
								}
							}
						})
						// #endif
					}
				})
			},
			isFullImg: function() {
				return new Promise((res) => {
					uni.showModal({
						content: "已经有9张图片了,是否清空现有图片？",
						success: (e) => {
							if (e.confirm) {
								this.imageList = [];
								res(true);
							} else {
								res(false)
							}
						},
						fail: () => {
							res(false)
						}
					})
				})
			},
			previewImage: function(e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.imageList
				})
			},
			async checkPermission(code) {
				let type = code ? code - 1 : this.sourceTypeIndex;
				let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
					await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
						'android.permission.READ_EXTERNAL_STORAGE');

				if (status === null || status === 1) {
					status = 1;
				} else {
					uni.showModal({
						content: "没有开启权限",
						confirmText: "设置",
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppSetting();
							}
						}
					})
				}

				return status;
			},
			bindUnitPickerChange: function(e) {
				this.unitIndex = e.detail.value;
				this.unit.id = this.unitArray[this.unitIndex].id;
				this.unit.name = this.unitArray[this.unitIndex].name;
			},
			delImage(e) {
				this.imageList.splice(e.target.dataset.src,1);
			},
			toLeftImage(e){
				if(e.target.dataset.src>0)
					this.imageList[e.target.dataset.src] = this.imageList.splice(e.target.dataset.src-1, 1, this.imageList[e.target.dataset.src])[0]
			},
			toRightImage(e){
				if(e.target.dataset.src+1<this.imageList.length)
					this.imageList[e.target.dataset.src] = this.imageList.splice(e.target.dataset.src+1, 1, this.imageList[e.target.dataset.src])[0]
			},
			changeProductName(e){
				this.productName = e.detail.value;
			},
			changeProductSellPoint(e){
				this.productSellPoint = e.detail.value;
			},
			addSku(e){
				let tmp = {name:'',pro:''};
				this.skuArray.push(tmp);
			},
			delSku(e){
				this.skuArray.splice(e.target.dataset.src,1);
			},
			changeSkuName(e){
				this.skuArray[e.target.dataset.src].name = e.detail.value;
			},
			changeSkuPro(e){
				if(e.detail.value==''||e.detail.value==null){

				}else{
					if(this.skuArray[e.target.dataset.src].name==''||this.skuArray[e.target.dataset.src].name==null){
						uni.showToast({
							title: '请填写规格名称',
							icon: 'none'
						});
					}
					this.skuDetailArray=[];
					this.skuArray[e.target.dataset.src].pro = e.detail.value;
						if(this.skuArray.length==1){
							for(let j=0;j<this.skuArray[0].pro.split("#").length; j++){
								let tmp = {retailPrice:'',costPrice:'',stock:'',name:''};
								tmp.name = this.skuArray[0].pro.split("#")[j];
								this.skuDetailArray.push(tmp);
							}	
						}
						if(this.skuArray.length==2){
							
							for(let j=0;j<this.skuArray[0].pro.split("#").length; j++){
								for(let k=0;k<this.skuArray[1].pro.split("#").length; k++){
									let tmp = {retailPrice:'',costPrice:'',stock:'',name:''};
									tmp.name = this.skuArray[0].pro.split("#")[j]+"-"+this.skuArray[1].pro.split("#")[k];									
									this.skuDetailArray.push(tmp);									
								}
							}	
						}
						if(this.skuArray.length==3){
							for(let j=0;j<this.skuArray[0].pro.split("#").length; j++){
								for(let k=0;k<this.skuArray[1].pro.split("#").length; k++){
									for(let m=0;m<this.skuArray[2].pro.split("#").length; m++){
										let tmp = {retailPrice:'',costPrice:'',stock:'',name:''};
										tmp.name = this.skuArray[0].pro.split("#")[j]+"-"+this.skuArray[1].pro.split("#")[k]+"-"+this.skuArray[2].pro.split("#")[m];										
										this.skuDetailArray.push(tmp);									
									}
								}
							}		
						}
				}
				
			},
			changeSkuDetailRetail(e){
				this.skuRetailPrice = '';
				this.skuDetailArray[e.target.dataset.src].retailPrice = e.detail.value;
				let tmp = 0;
				for(let i=0;i<this.skuDetailArray.length;i++){
					if(this.skuDetailArray[i].retailPrice==''){
						tmp =1;
						break;
					}
				}
				if(tmp==0){
					this.skuRetailPrice = Math.min.apply(Math,this.skuDetailArray.map(item => { return item.retailPrice }));
				}
			},
			changeSkuDetailCost(e){
				this.skuDetailArray[e.target.dataset.src].costPrice = e.detail.value;
			},
			changeSkuDetailWeight(e){
				this.skuDetailArray[e.target.dataset.src].weight = e.detail.value;
			},
			changeSkuDetailStock(e){
				this.skuStock = '';
				this.skuDetailArray[e.target.dataset.src].stock = e.detail.value;
				let tmp = 0;
				for(let i=0;i<this.skuDetailArray.length;i++){
					if(this.skuDetailArray[i].stock==''){
						tmp =1;
						break;
					}
				}
				if(tmp==0){
					this.skuStock  = 0;
					for(let item in this.skuDetailArray){
						this.skuStock = parseInt(this.skuStock) + parseInt(this.skuDetailArray[item].stock);  
					}
				}
			},
			changeSkuRetail(e){
				this.skuRetailPrice = e.detail.value;
			},
			changeSkuWeight(e){
				this.skuWeight = e.detail.value;
			},
			changeSkuCost(e){
				this.skuCostPrice = e.detail.value;
			},
			changeSkuStock(e){
				this.skuStock = e.detail.value;
			},
			changeExpressFee(e){
				this.expressFee = e.detail.value;
			},
			getInitData(){
				const req = {
					action : myConst.ACTION.QUERY,
					params: {}
				}
				myCloud.callFunction(myConst.FUNC_NAME.B_GOODS_GROUP,req, (data, e) => {
					let tmp1 = {text: '编辑'}
					let tmp2 = {text: '删除',style: {backgroundColor: 'rgb(255,58,49)'}}
					this.$data.swipeList = [];
					for(let i=0;i<data.length;i++){
						let d = {id:'',options:[],content:''}
						d.id = data[i]._id;
						d.content = data[i].groName;
						d.options.push(tmp1);
						d.options.push(tmp2);
						
						
						this.$data.swipeList.push(d);
					}
				},true);
				myCloud.callFunction(myConst.FUNC_NAME.B_SHIPPING_TEMPLATE,req, (data, e) => {
					for(let i=0;i<data.length;i++){
						let d = {id:'',name:'',billingMode:''}
						d.id = data[i]._id;
						d.name = data[i].temName;
						d.billingMode = data[i].billingMode;
						this.expressTemArray.push(d)
					}
					console.log(this.expressTemArray)
					if(this.action==myConst.ACTION.ADD){
						this.expressTemSelected = this.expressTemArray[0]
						this.expressTemIndex = 0;
					}
					// for(let item in this.expressTemArray){
					// 	if(this.expressTemArray[item].id==this.expressTemSelected.id){
					// 		this.expressTemIndex = item;
					// 		break;
					// 	}
					// }
				},false)
			},
			toSelectProductGroup(e){
				this.$refs.zxGoodsGroup.showGroupList();
			},
			//e name  v id
			setSelectProGroupValue(e,v){
				let tmp = {name:e,id:v}
				this.groupList.push(tmp);
			},
			delSelectProGroupValue(e){
				for(let i=0;i<this.groupList.length;i++){
					if(this.groupList[i].name==e){
						this.groupList.splice(i,1);
						break;
					}
				}
			},
			toAddProductGroup(){
				uni.navigateTo({
					url: `/pages/goods/goods-group-edit`
				})
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			changeDeliveryValue(e){
				this.delivery = e.detail.value;
			},
			changeExpressValue(e){
				this.express = e.detail.value;
			},
			changeExpressTemValue(e){
				this.expressTemIndex = e.detail.value
				this.expressTemSelected.id = this.expressTemArray[this.expressTemIndex].id
				this.expressTemSelected.name = this.expressTemArray[this.expressTemIndex].name
				this.expressTemSelected.billingMode =  this.expressTemArray[this.expressTemIndex].billingMode
			},
			changeShowStateValue(e){
				this.showState = e.detail.value;
			},
			toEditProDetail(){
				this.nav = 'next'
				this.formSubmit()
			},
			toSubmit(){
				this.nav= 'back'
				this.formSubmit()
			}
		}
	}
</script>

<style>
	/* #ifndef APP-PLUS-NVUE */
	/* css/uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	 
	
	/* 以下样式用于 hello uni-app 演示所需 */
	page {
	    background-color: #F4F5F6;
	    height: 100%;
	    font-size: 28upx;
	    line-height: 1.8;
	}
	
	.uni-header-logo {
	    padding: 30upx;
	    flex-direction: column;
	    justify-content: center;
	    align-items: center;
	    margin-top: 10upx;
	}
	
	.uni-header-image {
	    width: 100px;
	    height: 100px;
	}
	
	.uni-hello-text {
	    color: #7A7E83;
	}
	
	.uni-hello-addfile {
	    text-align: center;
	    line-height: 300upx;
	    background: #FFF;
	    padding: 50upx;
	    margin-top: 10px;
	    font-size: 38upx;
	    color: #808080;
	}
	
	/* #endif*/
	.cell-pd {
		padding: 22upx 30upx;
	}

	.list-pd {
		margin-top: 50upx;
	}
	
	.uni-input {
		height: auto;
	}
	
	.image-del{
		position: absolute;
		margin-top: 140upx;
		margin-left: 140upx;
		width: 70upx;
		height: 70upx;
	}
	.image-left{
		position: absolute;
		margin-top: 140upx;
		width: 70upx;
		height: 70upx;
	}
	.image-right{
		position: absolute;
		margin-top: 140upx;
		margin-left: 70upx;
		width: 70upx;
		height: 70upx;
	}
	.uni-uploader__img{
		position: absolute;
	}
	.v-img-bak{
		position: absolute;
		margin-top: 140upx;
		width: 210upx;
		height:70upx;
		background-color: #bfbfbf; 
		opacity: 0.6;
	}
	.uni-common-mt{
		margin-top: 0upx;
	}
	.list-pd {
		margin-top: 20upx;
	}
	.uni-padding-wrap{
		margin-top: 20upx;
		margin-bottom: 60upx;
	}
	.t-fill{
		color: #FF0000;
		position: absolute;
		left: 10rpx;
	}
	.v-select-mul-item{
	    /* display: inline-block; */
	    font-size: 0;
	    line-height: 60rpx;
	    margin-right: 10px;
	    outline: none;
	    position: relative;
	    text-align: left;
	    -webkit-user-select: none;
	    -moz-user-select: none;
	    -ms-user-select: none;
	    user-select: none;
	    vertical-align: middle;
	}
	.v-select-mul-con{
		background-color: #fff;
	    box-sizing: border-box;
	    /* display:inline; */
	    min-height: 30px;
	    max-height: 76px;
	    outline: none;
	    /* padding: 5px 5px; */
	    transition: border-color .25s;
	    width: 100%;
	    position: relative;
	}
	.t-select-mul-value{
	    border: 1px solid #c8c9cc;
	    border-color: var(--theme-stroke-4,#c8c9cc);
	    border-radius: 2px;
	    display: inline;
	    margin: 6px 4px 2px 0;
	    padding: 0 5px 0 4px;
	    /* line-height: 16px; */
	    font-size: 14px;
	    position: relative;
	}
	.uni-uploader__input-box{
		width: 206rpx;
	}
</style>